<?php
/* P3(c)

******************************************************************************

__
/__)
/__        P3 - moving Technologie
__)
__)


Copyright 2009 by p3.co.at

visit www.p3.co.at to get more Informations
about Teriko - Betriebsdatenerfassung
- it's not allowed to change this scripts
- it's not allowed to remove this copyright-tag

-- you can download the complete license
under http://www.teriko.org

(c) by Patrik Pfaffenbauer and P3 (http://www.p3.co.at)

| Programname: P3.Teriko
| Authtor: P3.Verein
| Coder: Patrik Pfaffenbauer
| Version: 0.1

| Change Index
|_____________________________________________
|                                             |
|4.3.2010 Erstellung | Patrik Pfaffenbauer    |
|_____________________________________________|

******************************************************************************

P3(c) */

class Task extends Base
{
	public function Task()
	{
		$this->title = $GLOBALS['lang']->GetLanguage("Task", "title");
		$this->toolbar = true;
		$this->moduleID = 310;
		$this->permission = GetPermission($this->moduleID);

		if(PermissionCount($this->permission) == 0)
		{
			$this->allowed = false;
		}
	}

	private function CheckInput()
	{
		include("./_tables.php");

		if($_POST['name'] == "")
		{
			$this->errorText = $GLOBALS['lang']->GetLanguage("Panel", "missedMandatoryField");
			return false;
		}

		$query = "SELECT * FROM ".$tbljobs." WHERE name='".$_POST['name']."' AND projectID=".$_POST['projectID'];

		if($GLOBALS['db']->Query($query)->num_rows)
		{
			$res = $GLOBALS['db']->Fetch($GLOBALS['db']->Query($query));

			if($_POST['id'] != $res->id)
			{
				$string = $GLOBALS['lang']->GetLanguage("Panel", "valueAlreadyExist");
				$string = str_replace("{0}", $GLOBALS['lang']->GetLanguage("Project", "name"), $string);
				$string = str_replace("{1}", $_POST['name'], $string);

				$this->errorText = $string;
				return false;
			}
		}

		return true;
	}
	public function CheckFastEditInput()
	{
		include("./_tables.php");

		if($_GET['field'] == "name")
		{
			$res = $GLOBALS['db']->Fetch($GLOBALS['db']->Query("SELECT * FROM ".$tbljobs." WHERE id=".$_GET['id']));
				
			$query = $GLOBALS['db']->Query("SELECT * FROM ".$tbljobs." WHERE name='".$_GET['value']."' AND projectID=".$res->projectID);
				
			if($GLOBALS['db']->NumRow($query))
			{
				$res = $GLOBALS['db']->Fetch($query);
					
				if($_GET['id'] != $res->id)
				{
					$string = $GLOBALS['lang']->GetLanguage("Panel", "valueAlreadyExist");
					$string = str_replace("{0}", $GLOBALS['lang']->GetLanguage("Project", "name"), $string);
					$string = str_replace("{1}", $_GET['value'], $string);
						
					echo "//<script>
					alert('".$string."');
					Redirect('index.php?s=task');
					//</script>";
					return false;
				}
			}
		}

		return true;
	}

	public function Add()
	{
		include("./_tables.php");
		$panel = new Panel();

		if($this->error)
		{
			$panel->AddRow();
			$panel->AddText($this->errorText);
		}

		$panel->AddRow();
		$panel->AddComboBox("projectID", $GLOBALS['lang']->GetLanguage("Project", "title"), $_POST['projectID'],  $tblproject, "id", "name");
		$panel->AddRow();
		$panel->AddTextBox("name", $GLOBALS['lang']->GetLanguage("Task", "name"), $_POST['name'], 45, 50);
		$panel->AddRow();
		$panel->AddTextArea("description", $GLOBALS['lang']->GetLanguage("Common", "description"), $_POST['description'], 50, 5);
		$panel->AddRow();
		$panel->AddDateTextBox("beginDate", $GLOBALS['lang']->GetLanguage("Common", "beginDate"), $_POST['beginDate'], 45, 30);
		$panel->AddRow();
		$panel->AddDateTextBox("endDate", $GLOBALS['lang']->GetLanguage("Common", "endDate"),$_POST['endDate'], 45, 30);

		$panel->AddHidden("action", "save");
		$panel->AddHidden("option", "project");

		$panel->Close();
	}
	public function AddToProject($projectID)
	{
		include("./_tables.php");
		$panel = new Panel();
		$panel->type = "save";

		$panel->AddRow();
		$panel->AddComboBox("projectID", $GLOBALS['lang']->GetLanguage("Project", "title"), $projectID,  $tblproject, "id", "name");
		$panel->AddRow();
		$panel->AddTextBox("name", $GLOBALS['lang']->GetLanguage("Task", "name"), "", 45, 50);
		$panel->AddRow();
		$panel->AddTextArea("description", $GLOBALS['lang']->GetLanguage("Common", "description"), "", 50, 5);
		$panel->AddRow();
		$panel->AddDateTextBox("beginDate", $GLOBALS['lang']->GetLanguage("Common", "beginDate"), "", 45, 30);
		$panel->AddRow();
		$panel->AddDateTextBox("endDate", $GLOBALS['lang']->GetLanguage("Common", "endDate"), "", 45, 30);

		$panel->AddHidden("action", "save");
		$panel->AddHidden("option", "project");

		$panel->Close();
	}

	public function ShowTasksByProject($projectID)
	{

		include("./_tables.php");

		echo
		'<div id="cpanel">';

		if($this->filter != "")
		$this->filter == $projectID;

		$dataSource = $GLOBALS['db']->Query("SELECT id as id, name as value FROM ".$tblproject." ");

		$dgv = new DataGridView($this);
		$dgv->AddComboBoxFilter($dataSource, $GLOBALS['lang']->GetLanguage("Task", "filter").": ", $this->filter , "projectID");

		$dgv->OpenHeader();
		$dgv->AddHeader($GLOBALS['lang']->GetLanguage("Task", "name"), "17%", true);
		$dgv->AddHeader($GLOBALS['lang']->GetLanguage("Common", "description"), "25%", true);
		$dgv->AddHeader($GLOBALS['lang']->GetLanguage("Common", "beginDate"), "17%", true);
		$dgv->AddHeader($GLOBALS['lang']->GetLanguage("Common", "endDate"), "17%", true);
		$dgv->AddHeader($GLOBALS['lang']->GetLanguage("Project", "name"), "17%", true);

		$dgv->CloseHeader();

		if($this->filter != "")
		{
			if(isset($_POST['filterID']))
			$query = "SELECT *,DATE_FORMAT(endDate,'%d.%m.%Y') as end,DATE_FORMAT(beginDate,'%d.%m.%Y') as begin FROM ".$tbljobs." WHERE ".$_POST['filterID']."=".$this->filter."";
			else
			$query = "SELECT *,DATE_FORMAT(endDate,'%d.%m.%Y') as end,DATE_FORMAT(beginDate,'%d.%m.%Y') as begin FROM ".$tbljobs;
				
			$res = $GLOBALS['db']->Query($query);
		}
		else
		{
			$res = $GLOBALS['db']->Query("SELECT *,DATE_FORMAT(endDate,'%d.%m.%Y') as end,DATE_FORMAT(beginDate,'%d.%m.%Y') as begin FROM ".$tbljobs." WHERE projectID=".$projectID);
		}

		if($this->permission->editPermission)
		{
			$fastEdit = "fastEdit";
			$comboBoxEdit = "comboBox-edit";
		}
		else
		{
			$fastEdit = "";
			$comboBoxEdit = "";
		}

		$count = 0;

		$query = $GLOBALS['db']->Query("SELECT id,name FROM ".$tblproject);
		while($line = $GLOBALS['db']->Fetch($query))
		{
			$object[] = $line;
		}

		while($line = $GLOBALS['db']->Fetch($res))
		{
			$dgv->AddRow($line->id);
			$dgv->AddData($line->name, $title, "", "name");
			$dgv->AddData(ShortText($line->description, 50, " ..."), "", "", "firstName");
			$dgv->AddData($line->begin, "date-fastEdit", "", "beginDate");
			$dgv->AddData($line->end, "", "", "endDate");
				
			$line2 = $GLOBALS['db']->Fetch($GLOBALS['db']->Query("SELECT * FROM ".$tblproject." WHERE id='".$line->projectID."'"));
			$dgv->object = $object;
			$dgv->object_id = $line->projectID;
			$dgv->AddData($line2->name, $comboBoxEdit, "", "projectID");

				
			$dgv->CloseRow();
				
			$count++;
		}

		$dgv->CloseDataGrid();

		echo '</div>';
		/*include("./_tables.php");

		echo
		'<div id="cpanel">';

		$dgv = new DataGridView($this);
		$dgv->AddHeader($GLOBALS['lang']->GetLanguage("Task", "name"), "17%", true);
		$dgv->AddHeader($GLOBALS['lang']->GetLanguage("Common", "description"), "25%", true);
		$dgv->AddHeader($GLOBALS['lang']->GetLanguage("Common", "beginDate"), "17%", true);
		$dgv->AddHeader($GLOBALS['lang']->GetLanguage("Common", "endDate"), "17%", true);
		$dgv->AddHeader($GLOBALS['lang']->GetLanguage("Project", "name"), "17%", true);

		$dgv->CloseHeader();

		$query = "SELECT *,DATE_FORMAT(endDate,'%d.%m.%Y') as end,DATE_FORMAT(beginDate,'%d.%m.%Y') as begin FROM ".$tbljobs." WHERE projectID='".$projectID."'";
		$res = $GLOBALS['db']->Query($query);

		if($this->permission->editPermission)
		$title = "fastEdit";
		else
		$title = "";

		$count = 0;

		while($line = mysql_fetch_object($res))
		{
		$dgv->AddRow($line->id);
		$dgv->AddData($line->name, $title, "", "name");
		$dgv->AddData(ShortText($line->description, 50, " ..."), "", "", "firstName");
		$dgv->AddData($line->begin, "", "", "beginDate");
		$dgv->AddData($line->end, "", "", "endDate");
			
		$line2 = Fetch($GLOBALS['db']->Query("SELECT * FROM ".$tblproject." WHERE id='".$line->projectID."'"));
		$dgv->AddData($line2->name, "", "", "projectID");

		$dgv->CloseRow();
			
		$count++;
		}

		$dgv->AddHidden("projectID", $projectID);
		$dgv->CloseDataGrid();

		echo '</div>';*/
	}

	public function Edit()
	{
		include("./_tables.php");

		$res = $GLOBALS['db']->Fetch($GLOBALS['db']->Query("SELECT *,DATE_FORMAT(endDate,'%d.%m.%Y') as end,DATE_FORMAT(beginDate,'%d.%m.%Y') as begin FROM ".$tbljobs." WHERE id=".$_POST['id'].""));

		$panel = new Panel();

		if(isset($_POST['name']))
		{
			if($this->error)
			{
				$panel->AddRow();
				$panel->AddText($this->errorText);
			}
				
			$res->projectID = $_POST['projectID'];
			$res->name = $_POST['name'];
			$res->description = $_POST['description'];
			$res->beginDate = $_POST['beginDate'];
			$res->endDate = $_POST['endDate'];
		}

		$panel->AddRow();
		$panel->AddComboBox("projectID", $GLOBALS['lang']->GetLanguage("Project", "title"), $res->projectID,  $tblproject, "id", "name");
		$panel->AddRow();
		$panel->AddTextBox("name", $GLOBALS['lang']->GetLanguage("Task", "name"), $res->name, 45, 50);
		$panel->AddRow();
		$panel->AddTextArea("description", $GLOBALS['lang']->GetLanguage("Common", "description"), $res->description, 50, 5);
		$panel->AddRow();
		$panel->AddDateTextBox("beginDate", $GLOBALS['lang']->GetLanguage("Common", "beginDate"), $res->begin, 45, 30);
		$panel->AddRow();
		$panel->AddDateTextBox("endDate", $GLOBALS['lang']->GetLanguage("Common", "endDate"), $res->end, 45, 30);

		$panel->AddHidden("action", "update");
		$panel->AddHidden("option", "project");
		$panel->AddHidden("id", $_POST['id']);

		$panel->Close();
	}

	public function AnalysPost()
	{
		include("./_tables.php");
		$this->filter = $_POST['search'];
			
		if($_POST['action'] == "add")
		{
			if(isset($_POST['projectID']))
			{
				$this->AddToProject($_POST['projectID']);
			}
			else if(isset($_POST['search']))
			{
				$this->AddToProject($_POST['search']);
			}
			else
			{
				$this->Add();
			}
		}
		else if($_POST['action'] == "edit")
		{
			$this->Edit();
		}
		else if($_POST['action'] == "search")
		{
			$this->Paint();
		}
		else if($_POST['action'] == "paint")
		{
			$this->filter = "";
			$this->Paint();
		}
		else if($_POST['action'] == "activate")
		{
			$this->Activate();
		}
		else if($_POST['action'] == "save")
		{
			$_POST['beginDate'] = ConvertDate($_POST['beginDate'], ".");
			$_POST['endDate'] = ConvertDate($_POST['endDate'], ".");
				
			if($this->CheckInput())
			{
				$GLOBALS['db']->SaveObject($tbljobs);
				$this->Paint();
				$this->error = false;
			}
			else
			{
				$this->error = true;
				$this->Add();
			}
		}
		else if($_POST['action'] == "update")
		{
			$_POST['beginDate'] = ConvertDate($_POST['beginDate'], ".");
			$_POST['endDate'] = ConvertDate($_POST['endDate'], ".");
				
			if($this->CheckInput())
			{
				$GLOBALS['db']->UpdateObject($tblproject);
				$this->Paint();
				$this->error = false;
			}
			else
			{
				$this->error = true;
				$this->Edit();
			}
		}
		else if($_POST['action'] == "delete")
		{
			$GLOBALS['db']->Delete($tbljobs);
			$this->Paint();
		}
		else
		{
			$this->Paint();
		}
	}

	public function Paint()
	{
		include("./_tables.php");

		echo
		'<div id="cpanel">';

		$dataSource = $GLOBALS['db']->Query("SELECT id as id, name as value FROM ".$tblproject." ");

		$dgv = new DataGridView($this);
		$dgv->AddComboBoxFilter($dataSource, $GLOBALS['lang']->GetLanguage("Task", "filter").": ", $this->filter, "projectID");

		$dgv->OpenHeader();
		$dgv->AddHeader($GLOBALS['lang']->GetLanguage("Task", "name"), "17%", true);
		$dgv->AddHeader($GLOBALS['lang']->GetLanguage("Common", "description"), "25%", true);
		$dgv->AddHeader($GLOBALS['lang']->GetLanguage("Common", "beginDate"), "17%", true);
		$dgv->AddHeader($GLOBALS['lang']->GetLanguage("Common", "endDate"), "17%", true);
		$dgv->AddHeader($GLOBALS['lang']->GetLanguage("Project", "name"), "17%", true);

		$dgv->CloseHeader();

		if($this->filter != "")
		{
			if(isset($_POST['filterID']))
			$query = "SELECT *,DATE_FORMAT(endDate,'%d.%m.%Y') as end,DATE_FORMAT(beginDate,'%d.%m.%Y') as begin FROM ".$tbljobs." WHERE ".$_POST['filterID']."=".$this->filter."";
			else
			$query = "SELECT *,DATE_FORMAT(endDate,'%d.%m.%Y') as end,DATE_FORMAT(beginDate,'%d.%m.%Y') as begin FROM ".$tbljobs;
				
			$res = $GLOBALS['db']->Query($query);
		}
		else
		{
			$res = $GLOBALS['db']->Query("SELECT *,DATE_FORMAT(endDate,'%d.%m.%Y') as end,DATE_FORMAT(beginDate,'%d.%m.%Y') as begin FROM ".$tbljobs);
		}

		if($this->permission->editPermission)
		{
			$fastEdit = "fastEdit";
			$comboBoxEdit = "comboBox-edit";
		}
		else
		{
			$fastEdit = "";
			$comboBoxEdit = "";
		}

		$count = 0;

		$query = $GLOBALS['db']->Query("SELECT id,name FROM ".$tblproject);
		while($line = $GLOBALS['db']->FetchRow($query))
		{
			$object[] = $line;
		}

		while($line = $GLOBALS['db']->Fetch($res))
		{
			$dgv->AddRow($line->id);
			$dgv->AddData($line->name, $fastEdit, "", "name");
			$dgv->AddData(ShortText($line->description, 50, " ..."), "", "", "firstName");
			$dgv->AddData($line->begin, "", "", "beginDate");
			$dgv->AddData($line->end, "", "", "endDate");
				
			$line2 = $GLOBALS['db']->Fetch($GLOBALS['db']->Query("SELECT * FROM ".$tblproject." WHERE id='".$line->projectID."'"));
			$dgv->object = $object;
			$dgv->object_id = $line->projectID;
			$dgv->AddData($line2->name, $comboBoxEdit, "", "projectID");

				
			$dgv->CloseRow();
				
			$count++;
		}

		$dgv->CloseDataGrid();

		echo '</div>';
	}
	public function Toolbar()
	{
		if(!$this->allowed)
		return;
			
		PaintDefaultToolbar($this);
	}

	public function AddCSS()
	{
			
	}
	public function AddJS()
	{

	}
}

?>